<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctxPath}/assets/common/libs/layui/css/layui.css?v=${constants.getReleaseVersion()}"/>
    <link rel="stylesheet" href="${ctxPath}/assets/common/module/admin.css?v=${constants.getReleaseVersion()}" media="all"/>
    <link rel="stylesheet" href="${ctxPath}/assets/expand/css/style.css?v=${constants.getReleaseVersion()}" media="all"/>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form id="policyForm" lay-filter="policyForm" class="layui-form model-form" style="padding: 40px 30px 0 0;">
    <input type="hidden" name="policyUrl1" id="policyUrl1">
    <input type="hidden" name="id" id="id" value="${id}">
    <input type="hidden" name="url" id="url" value="${url}">
    <div class="layui-form-item">
        <label class="layui-form-label">选择保单:</label>
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test3">请选择</button>
            <div class="layui-upload-list">
                <img class="layui-upload-img" id="demo1">
                <p id="demoText" style="margin-left: 40px"></p>
            </div>
        </div>
    </div>

    <div class="layui-form-item text-right">
        <a class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</a>
        <a class="layui-btn" lay-filter="btnSubmit" lay-submit>保存</a>
    </div>
</form>
<script type="text/javascript">
    var Feng = {
        ctxPath: "${ctxPath}",
        version: '${constants.getReleaseVersion()}'
    };
</script>

<script type="text/javascript" src="${ctxPath}/assets/common/libs/layui/layui.js?v=${constants.getReleaseVersion()}"></script>
<script type="text/javascript" src="${ctxPath}/assets/common/js/common.js?v=${constants.getReleaseVersion()}"></script>
<style type="text/css">
    .layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 40px;}
</style>
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'ax','upload'], function () {
        var $ = layui.$;
        var layer = layui.layer;
        var form = layui.form;
        var $ax = layui.ax;
        var admin = layui.admin;
        var upload = layui.upload;

        // 让当前iframe弹层高度适应
        admin.iframeAuto();

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test3'
            ,accept:'file'
            ,url: Feng.ctxPath + '/dfsfile/upload'//改成您自己的上传接口
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    if(file.type == 'application/pdf'){
                        $("#demoText").text(file.name);
                        $('#demo1').css("display","none");
                    } else {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    }
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code == 200){
                    $("#policyUrl1").val(res.data.filePath);
                } else {
                    layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });

        // 表单提交事件
        form.on('submit(btnSubmit)', function (data) {
            var policyUrl1 = $("#policyUrl1").val();
            if(policyUrl1 == ''){
                Feng.error("请上传保单");
                return false;
            }
            var url = $("#url").val();
            var ajax = new $ax(Feng.ctxPath + "/"+url+"/policy", function (data) {
                Feng.info("上传保单成功!");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);

                //关掉对话框
                admin.closeThisDialog();
            }, function (data) {
                Feng.error("上传保单失败!" + data.responseJSON.message + "!");
            });
            ajax.set("policyNo", '');
            ajax.set("policyUrl1", policyUrl1);
            ajax.set("policyUrl2", '');
            ajax.set("id", $("#id").val());
            ajax.start();
        });
    });


</script>
</body>
</html>